import { Item } from "../../shared/face/index";
import { BaseBody } from '../../core/rpg';
import { BagFuncResult } from './types';
import { ComType } from "../../core/tool/component";
import talent_component from "../../component/talent_component";
import { TalentService } from "../TalentService";

/**
 * AddTalent 功能
 * @param useItem 使用的物品
 * @param useCount 使用数量
 * @param funcData 函数数据
 * @param player 玩家对象
 * @returns 使用结果
 */
export async function AddTalent(useItem: Item, useCount: number, funcData: any, player: BaseBody): Promise<BagFuncResult> {
    try {
        // 添加血统
        let talentCom = player.getComponent(ComType.血统) as talent_component;
        let result = TalentService.addTalent(talentCom.list, funcData.id, useCount);
        if (!result.success) {
            return {
                success: false,
                message: result.message,
                data: { action: 'AddTalent', error: '血统添加失败' }
            };
        } else {
            talentCom.list = result.newTalentList;
            if (result.levelChanged) {
                talentCom.fighting_end();
            }
        }
        return {
            success: true,
            message: result.message,
            data: { action: 'AddTalent' }
        };
    } catch (error: any) {
        return {
            success: false,
            message: '功能执行失败',
            data: { error: error.message }
        };
    }
}
